package com.zm.schedule.schedulemgr.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.zm.base.core.exception.BaseException;
import com.zm.base.core.po.ZmResult;
import com.zm.schedule.schedulemgr.po.TbZmScheduleJob;
import com.zm.schedule.schedulemgr.service.IScheduleService;
import com.zm.schedule.schedulemgr.service.ISchedulerBuilder;
import com.zm.schedule.schedulemgr.vo.TbZmScheduleJobVO;

/**
 * @author chenhuaming
 * @Time：2017年5月8日 上午10:09:22
 * @version 1.0
 */
@Controller
@RequestMapping("/schedule")
public class ScheduleController {
	@Resource(name="scheduleService")
	private IScheduleService scheduleService;
	@Resource(name="schedulerBuilder")
	private ISchedulerBuilder schedulerBuilder;
	/**
	 * 到添加也页面
	 * toScheduleSave:() 
	 * @author chenhuaming 
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/toSave")
	public String toScheduleSave(){
		return "/schedule/add";
	}
	/**
	 * 保存任务（新增或者修改）
	 * saveSchedule:() 
	 * @author chenhuaming 
	 * @param schedule
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/save")
	@ResponseBody
	public ZmResult saveSchedule(TbZmScheduleJob schedule){
		ZmResult result=new ZmResult();
		try {
			//scheduleService.saveSchedule(schedule);
			if(!StringUtils.isEmpty(schedule.getJobId())){
				scheduleService.updateSchedule(schedule);
				result.setStatus(200);
				result.setMsg("保存成功");
			}else{
				TbZmScheduleJob tbZmScheduleJob=scheduleService.getJobByJobNameAndJobGroup(schedule.getJobName(), schedule.getJobGroup());
				if(StringUtils.isEmpty(tbZmScheduleJob)){
					result.setStatus(-1);
					result.setMsg("保存失败");
				}else{
					scheduleService.saveSchedule(schedule);
					result.setStatus(200);
					result.setMsg("保存成功");
				}
			}
		} catch (BaseException e) {
			result.setStatus(-1);
			result.setMsg("保存失败");
			e.printStackTrace();
		} catch (Exception e) {
			result.setStatus(-1);
			result.setMsg("保存失败");
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 分页查询
	 * @author chenhuaming
	 * @Time 2017年5月11日
	 * @param pageNum
	 * @param jobName
	 * @param jobGroup
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/getScheduleByPage")
	@ResponseBody
	public ZmResult  getScheduleByPage(String pageNum,String jobName,String jobGroup){
		ZmResult result=new ZmResult();
		try {
			Page page=new Page();
			page.setPageNum(1);
			page.setPageSize(10);
			PageInfo<TbZmScheduleJobVO> jobs=scheduleService.getTbZmScheduleJobByPage(page, jobName, jobGroup);
			result.setStatus(200);
			result.setMsg("查询成功");
			result.setData(jobs);
		} catch (Exception e) {
			result.setStatus(-1);
			result.setMsg("查询失败");
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 获取一个job
	 * @author chenhuaming
	 * @Time 2017年5月12日
	 * @param id
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/getSchedule")
	@ResponseBody
	public ZmResult getSchedule(String id){
		ZmResult result=new ZmResult();
		try {
			TbZmScheduleJob job=scheduleService.getSchedule(id);
			result.setMsg("查询成功");
			result.setStatus(200);
			result.setData(job);
		} catch (Exception e) {
			result.setStatus(-1);
			result.setMsg("查询失败");
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 删除
	 * @author chenhuaming
	 * @Time 2017年5月12日
	 * @param id
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/delSchedule")
	@ResponseBody
	public ZmResult delSchedule(String[] ids){
		ZmResult result=new ZmResult();
		try {
			scheduleService.deleteSchedule(ids);
			result.setMsg("删除成功");
			result.setStatus(200);
		} catch (Exception e) {
			result.setMsg("删除失败");
			result.setStatus(-1);
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 停止
	 * @author chenhuaming
	 * @Time 2017年5月12日
	 * @param id
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/stopSchedule")
	@ResponseBody
	public ZmResult stopSchedule(String id){
		ZmResult result=new ZmResult();
		try {
			TbZmScheduleJob job=scheduleService.getSchedule(id);
			scheduleService.stopSchedule(job);
			result.setMsg("停止成功");
			result.setStatus(200);
		} catch (Exception e) {
			result.setMsg("停止失败");
			result.setStatus(-1);
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 暂停job
	 * @author chenhuaming
	 * @Time 2017年5月12日
	 * @param id
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/pauseSchedule")
	@ResponseBody
	public ZmResult pauseSchedule(String id){
		ZmResult result=new ZmResult();
		try {
			TbZmScheduleJob scheduleJob = scheduleService.getSchedule(id);
			schedulerBuilder.pauseJob(scheduleJob);
			result.setMsg("暂停成功");
			result.setStatus(200);
		} catch (Exception e) {
			result.setMsg("暂停失败");
			result.setStatus(-1);
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 恢复任务
	 * pauseSchedule:() 
	 * @author chenhuaming 
	 * @param id
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/recoverySchedule")
	@ResponseBody
	public ZmResult recoverySchedule(String id){
		ZmResult result=new ZmResult();
		TbZmScheduleJob scheduleJob;
		try {
			scheduleJob = scheduleService.getSchedule(id);
			schedulerBuilder.recoverJob(scheduleJob);
			result.setMsg("恢复成功");
			result.setStatus(200);
		} catch (Exception e) {
			result.setMsg("恢复失败");
			result.setStatus(-1);
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 启动任务
	 * startSchedule:() 
	 * @author chenhuaming 
	 * @param id
	 * @return
	 */
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value="/startSchedule")
	@ResponseBody
	public ZmResult startSchedule(String id){
		ZmResult result=new ZmResult();
		try {
			TbZmScheduleJob job=scheduleService.getSchedule(id);
			scheduleService.startSchedule(job);
			result.setMsg("启动成功");
			result.setStatus(200);
		} catch (BaseException e) {
			result.setMsg("启动失败");
			result.setStatus(-1);
			e.printStackTrace();
		} catch (Exception e) {
			result.setMsg("启动失败");
			result.setStatus(-1);
			e.printStackTrace();
		}
		return result;
	}
}
